import { defineConfig, loadEnv } from 'vite';
import uni from '@dcloudio/vite-plugin-uni';
import { resolve } from 'path';

const pathResolve = (dir: string) => {
	return resolve(process.cwd(), '.', dir);
};
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
	const env = loadEnv(mode, process.cwd());

	return {
		server: {
			// host: '192.168.1.15',
			strictPort: true,
			port: 9898,
			open: true,
			proxy: {
				/** 代理前缀为 /dev-api 的请求  */
				[env.VITE_APP_BASE_API]: {
					changeOrigin: true,
					// 接口地址
					target: env.VITE_APP_API_URL,
					rewrite: (path) => path.replace(new RegExp('^' + env.VITE_APP_BASE_API), '')
				}
			}
		},
		plugins: [uni()],
		resolve: {
			alias: [
				{
					find: '@',
					replacement: pathResolve('src') // 这里是将src目录配置别名为 @ 方便在项目中导入src目录下的文件
				},
				{
					find: '#',
					replacement: pathResolve('types')
				},
				{
					find: '@components',
					replacement: pathResolve('src/components')
				}
			]
		},
		build: {
			minify: 'terser',
			terserOptions: {
				compress: {
					drop_console: true //发布时删除 console
				}
			}
		}
	};
});
